package com.example.adv_android;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import org.greenrobot.eventbus.EventBus;

public class ExitDialog extends Dialog {

    private boolean showFlag = false;

    private TextView tv_name;

    private int index;

    public ExitDialog(@NonNull Context context) {
        super(context,R.style.DialogTheme);
        setContentView(R.layout.dialog_edit);

        tv_name = findViewById(R.id.tv_name);
        Button btn_OK = findViewById(R.id.btn_OK);
        Button btn_Cancel = findViewById(R.id.btn_cancel);
        EditText Edit_Price = findViewById(R.id.edit_price);


        btn_OK.setOnClickListener(v -> {
            String price = Edit_Price.getText().toString();
            if (price.isEmpty()) {
                Toast.makeText(context, "请输入价格", Toast.LENGTH_SHORT).show();
                return;
            }

            EventBus.getDefault().post(new EventMessage(index, price));

            dismissDialog();
        });

        btn_Cancel.setOnClickListener(v -> {
            dismissDialog();
        });
    }


    public void showDialog(int index) {
        if (showFlag){ return;}
        showFlag = true;
        this.index=index;
        tv_name.setText(MyApp.names[index] + "价格：");
        show();
//        Log.d("TAG", "showDialog: show dialog");
    }

    void dismissDialog() {
        showFlag = false;
        dismiss();
    }
}
